package com.fzh.springboot.utils;

import com.alibaba.fastjson.JSONObject;
import com.fzh.springboot.constant.ChatType;
import com.fzh.springboot.domain.ChatMessage;
import lombok.extern.slf4j.Slf4j;

import javax.websocket.Session;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
public class WebSocketUtil {
    public static final Map<String, Session> CLIENTS = new ConcurrentHashMap<>();

    public static void put(String key, Session value) {
        Session session = CLIENTS.get(key);
        if (session != null) {
            try {
                session.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        CLIENTS.put(key, value);
    }

    public static Session get(String key) {
        return CLIENTS.get(key);
    }

    public static void remove(String key) {
        CLIENTS.remove(key);
    }

    public static void sendMessageAll(ChatMessage chatMessage) {
        CLIENTS.forEach(((sessionId, session) -> sendMessage(session, chatMessage)));
    }

    public static void sendMessage(Session session, ChatMessage chatMessage) {
        if (session == null) return;
        if (chatMessage == null) return;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        chatMessage.setTime(simpleDateFormat.format(new Date()));
        try {
            String message = JSONObject.toJSONString(chatMessage);
            session.getBasicRemote().sendText(message);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("send message error: ", e);
        }
    }

    public static void onOpen(String sessionId, Session session) {
        log.info("用户连接：" + sessionId);
        WebSocketUtil.put(sessionId, session);
        ChatMessage chatMessage = new ChatMessage();
        chatMessage.setType(ChatType.CONNECT);
        chatMessage.setSessionId(sessionId);
        chatMessage.setMessage("用户" + sessionId + "连接");
        sendMessageAll(chatMessage);
        sendOnlineNumber();
    }

    public static void onClose(String sessionId, Session session) {
        log.info("用户断开连接：" + sessionId);
        WebSocketUtil.remove(sessionId);
        ChatMessage chatMessage = new ChatMessage();
        chatMessage.setType(ChatType.UN_CONNECT);
        chatMessage.setSessionId(sessionId);
        chatMessage.setMessage("用户" + sessionId + "断开连接");
        sendMessageAll(chatMessage);
        sendOnlineNumber();
    }

    public static void onMessage(String sessionId, String message, Session session) {
        log.info("用户：" + sessionId + ",发送消息：" + message);
        ChatMessage chatMessage = new ChatMessage();
        chatMessage.setType(ChatType.CHAT);
        chatMessage.setSessionId(sessionId);
        chatMessage.setMessage(message);
        sendMessageAll(chatMessage);
    }

    public static void onError(String sessionId, Session session, Throwable error) {
        log.info("用户连接错误：" + sessionId);
        error.printStackTrace();
    }

    public static void sendOnlineNumber() {
        ChatMessage chatMessage = new ChatMessage();
        chatMessage.setType(ChatType.ONLINE_COUNT);
        chatMessage.setMessage(String.valueOf(CLIENTS.size()));
        WebSocketUtil.sendMessageAll(chatMessage);
    }
}
